package com.mical.demo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class AsyncConfig {
    @Bean("sseTaskExecutor")
    public AsyncTaskExecutor sseTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(16); // 根据服务器配置调整
        executor.setMaxPoolSize(56);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("sse-exec-");
        // 高级配置
        executor.setAllowCoreThreadTimeOut(true); // 允许核心线程超时回收
        executor.setKeepAliveSeconds(30);      // 空闲线程存活时间
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }
}
